/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_dubbo_detect.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/08/15 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <dubbo/dubbo_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../../src/module/dubbo/config.h"

extern int dubbo_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    int i_ret = 0;

    i_ret = dubbo_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    return;
}

static void case2(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\xda\xbb\xc2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xba\x05\x32\x2e\x35\x2e\x33\x30\x22\x63\x6f\x6d\x2e\x61\x6c\x69\x62\x61\x62\x61\x2e\x64\x75\x62\x62\x6f\x2e\x64\x65\x6d\x6f\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x05\x30\x2e\x30\x2e\x30\x08\x73\x61\x79\x48\x65\x6c\x6c\x6f\x12\x4c\x6a\x61\x76\x61\x2f\x6c\x61\x6e\x67\x2f\x53\x74\x72\x69\x6e\x67\x3b\x06\x77\x6f\x72\x6c\x64\x30\x48\x04\x70\x61\x74\x68\x30\x22\x63\x6f\x6d\x2e\x61\x6c\x69\x62\x61\x62\x61\x2e\x64\x75\x62\x62\x6f\x2e\x64\x65\x6d\x6f\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x09\x69\x6e\x74\x65\x72\x66\x61\x63\x65\x30\x22\x63\x6f\x6d\x2e\x61\x6c\x69\x62\x61\x62\x61\x2e\x64\x75\x62\x62\x6f\x2e\x64\x65\x6d\x6f\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x07\x76\x65\x72\x73\x69\x6f\x6e\x05\x30\x2e\x30\x2e\x30\x5a";
    p_st_packet->ui_payload_len = 202;

    i_ret = dubbo_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case3(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\xda\xbb\x02\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x39\x91\x30\x36\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x30\x2c\x20\x72\x65\x73\x70\x6f\x6e\x73\x65\x20\x66\x6f\x72\x6d\x20\x70\x72\x6f\x76\x69\x64\x65\x72\x3a\x20\x31\x30\x2e\x31\x2e\x31\x30\x2e\x37\x38\x3a\x32\x30\x38\x38\x30";
    p_st_packet->ui_payload_len = 73;

    i_ret = dubbo_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

int add_suite_dubbo_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" dubbo_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
